﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Category
{
    /// <summary>
    /// 初始化乐器类别树
    /// </summary>
    /// <param name="treeView"></param>
    /// <param name="sNodeUrl"></param>
    public void InitCategory(TreeView treeView, string sNodeUrl)
    {
        Category category = new Category();
        DataTable dataTable = category.GetCategorys();
        treeView.Nodes.Clear();     ///清空树的所有节点

        DataRow[] rowList = dataTable.Select("parentID='-1'");
        if (rowList.Length <= 0) return;

        foreach (DataRow row in rowList)
        {
            ///创建根节点
            TreeNode rootNode = new TreeNode();
            ///设置根节点属性
            rootNode.Text = row["name"].ToString();
            rootNode.Value = row["categoryID"].ToString(); ///设置根节点的Key值
            rootNode.Expanded = true;
            rootNode.Selected = true;
            rootNode.NavigateUrl = sNodeUrl + "?CategoryID=" + rowList[0]["categoryID"].ToString();
            //rootNode.Target = "MainFrame";

            ///添加根节点
            treeView.Nodes.Add(rootNode);

            ///创建其他节点
            InitChildNode(rootNode, dataTable, sNodeUrl);
        }
        
    }
    /// <summary>
    /// 初始化乐器类别子树
    /// </summary>
    /// <param name="parentNode"></param>
    /// <param name="dataTable"></param>
    /// <param name="sNodeUrl"></param>
    private void InitChildNode(TreeNode parentNode, DataTable dataTable, string sNodeUrl)
    {
        ///选择数据时，添加了排序表达式OrderBy
        DataRow[] rowList = dataTable.Select("parentID='" + parentNode.Value + "'", "orderBy");
        foreach (DataRow row in rowList)
        {
            ///创建新节点
            TreeNode node = new TreeNode();
            ///设置节点的属性
            node.Text = row["Name"].ToString();
            node.Value = row["categoryID"].ToString();
            node.Expanded = true;
            node.NavigateUrl = sNodeUrl + "?CategoryID=" + row["CategoryID"].ToString();
            //node.Target = "MainFrame";

            parentNode.ChildNodes.Add(node);
            ///递归调用，创建其他节点
            InitChildNode(node, dataTable, sNodeUrl);
        }
    }
    /// <summary>
    /// 绑定乐器类别树
    /// </summary>
    /// <param name="treeView"></param>
    /// <param name="isExpanded"></param>
    /// <param name="sSelectedData"></param>
    public void BindCategoryTreeView(TreeView treeView, bool isExpanded, string sSelectedData)
    {
        Category category = new Category();
        DataTable dataTable = category.GetCategorys();
        treeView.Nodes.Clear();     ///清空树的所有节点

        DataRow[] rowList = dataTable.Select("ParentID='-1'");
        if (rowList.Length <= 0) return;

        ///创建根节点
        TreeNode rootNode = new TreeNode();
        ///设置根节点属性
        rootNode.Text = rowList[0]["Desn"].ToString();
        rootNode.Value = rowList[0]["CategoryID"].ToString(); ///设置根节点的Key值
        rootNode.Expanded = isExpanded;
        rootNode.Selected = true;

        ///添加根节点
        treeView.Nodes.Add(rootNode);

        ///创建其他节点
        CreateChildNode(rootNode, dataTable, isExpanded, sSelectedData);
    }
    /// <summary>
    /// 创建子树
    /// </summary>
    /// <param name="parentNode"></param>
    /// <param name="dataTable"></param>
    /// <param name="isExpanded"></param>
    /// <param name="sSelectedData"></param>
    private void CreateChildNode(TreeNode parentNode, DataTable dataTable, bool isExpanded,
       string sSelectedData)
    {
        ///选择数据时，添加了排序表达式OrderBy
        DataRow[] rowList = dataTable.Select("ParentID='" + parentNode.Value + "'", "OrderBy");
        foreach (DataRow row in rowList)
        {   ///创建新节点
            TreeNode node = new TreeNode();
            ///设置节点的属性
            node.Text = row["Desn"].ToString();
            node.Value = row["CategoryID"].ToString();
            node.Expanded = isExpanded;
            if (node.Value == sSelectedData)
            {
                node.Selected = true;
            }

            parentNode.ChildNodes.Add(node);
            ///递归调用，创建其他节点
            CreateChildNode(node, dataTable, isExpanded, sSelectedData);
        }
    }
    /// <summary>
    /// 绑定到下拉列表
    /// </summary>
    /// <param name="categroyList"></param>
    public void BindCategoryTreeList(DropDownList categoryList)
    {
        int nDepth = 0;
        Category category = new Category();
        DataTable dataTable = category.GetCategorys();
        categoryList.Items.Clear();     //清空树的所有节点

        DataRow row = dataTable.NewRow();
        row["name"] = "所有分类";
        row["categoryID"] = "-1";
        ///创建、添加根节点
        
        categoryList.Items.Add(new ListItem(row["name"].ToString(), row["categoryID"].ToString()));
        ///创建其他节点
        CreateChildNode(categoryList, dataTable, -1, nDepth);
    }
    /// <summary>
    /// 绑定子类别
    /// </summary>
    /// <param name="categroyList"></param>
    /// <param name="dataTable"></param>
    /// <param name="nParentID"></param>
    /// <param name="nDepth"></param>
    private void CreateChildNode(DropDownList categoryList, DataTable dataTable, int nParentID, int nDepth)
    {
        nDepth++;
        string sSpace = "";
        for (int i = 0; i < nDepth * 2; i++)
        {
            sSpace += "---";
        }

        ///选择数据时，添加了排序表达式OrderBy
        DataRow[] rowList = dataTable.Select("parentID='" + nParentID.ToString() + "'", "OrderBy");
        foreach (DataRow row in rowList)
        {
            ///创建新节点
            categoryList.Items.Add(new ListItem(sSpace + row["name"].ToString(),
                row["categoryID"].ToString()));

            ///递归调用，创建其他节点
            CreateChildNode(categoryList, dataTable, Int32.Parse(row["categoryID"].ToString()), nDepth);
        }
    }
    /// <summary>
    /// 获得所有的分类
    /// </summary>
    /// <returns></returns>
    public DataTable GetCategorys()
    {
        //声明定义类型和方法的DataModelDataContext类的对象，它是调用模型和方法的基础。
        DataModelDataContext data = new DataModelDataContext();
        //调用方法Pr_GetCategorys获得所有的分类
        var result = data.Pr_GetAllCategorys();
        ///定义保存从数据库获取的结果的DataTable
        DataTable table = new DataTable();
        //把LINQ查询结果集转化为DataTable
        table = result.CopyToDataTable();
        return table;
    }
    /// <summary>
    /// 获得子类别
    /// </summary>
    /// <param name="CategoryID"></param>
    /// <returns></returns>
    public DataTable GetSubCategorys(int CategoryID)
    {
        //声明定义类型和方法的DataModelDataContext类的对象，它是调用模型和方法的基础。
        DataModelDataContext data = new DataModelDataContext();
        //调用方法Pr_GetSubCategorys获得子类别
        var result = data.Pr_GetSubCategorys(CategoryID);
        ///定义保存从数据库获取的结果的DataTable
        DataTable table = new DataTable();
        //把LINQ查询结果集转化为DataTable
        table = result.CopyToDataTable();
        return table;
    }
    /// <summary>
    /// 获得某一类别
    /// </summary>
    /// <param name="CategoryID"></param>
    /// <returns></returns>
    public DataTable GetSingleCategory(int CategoryID)
    {
        //声明定义类型和方法的DataModelDataContext类的对象，它是调用模型和方法的基础。
        DataModelDataContext data = new DataModelDataContext();
        //调用方法Pr_GetSingleCategory获得某一类别
        var result = data.Pr_GetSingleCategory(CategoryID);
        ///定义保存从数据库获取的结果的DataTable
        DataTable table = new DataTable();
        //把LINQ查询结果集转化为DataTable
        table = result.CopyToDataTable();
        return table;
    }
}
